/*
 * Copyright (c) 2010-2011 Plugger Antonio Begue Ponce. All Rights Reserved.
 *
 * This file is part of Plugger Framework.
 *
 *  Plugger Framework is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License version 3 as published by
 *  the Free Software Foundation.
 *
 *  Plugger Framework is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Plugger Framework.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.plugger.rules;

import java.text.MessageFormat;
import org.plugger.infrastructure.domainbase.IValidatableForPersistence;
import org.plugger.infrastructure.rules.ValueBoundRule;

/**
 * Entity rule that validate the existence of a string property.<BR/>
 * <code>RequiredStringFieldRule(propertyName, candidate)<code/>
 *
 * @author Antonio Begue
 * @version $Revision: 1.0 $
 */
public class RequiredStringFieldRule extends ValueBoundRule<String> {
    /**
     * The RequiredStringFieldRule constructor.
     * @param attributeName The name of the property.
     * @param candidate The candidate object to be evaluated.
     */
    public RequiredStringFieldRule(String attributeName, IValidatableForPersistence candidate) {
        super(candidate, attributeName);
        setRuleId(this.getClass().getSimpleName()); // setRuleId("RequiredStringFieldRule");
        setErrorMessages(candidate.getBrokenRuleMessages());
    }

    @Override
    public boolean isValid() {
        String value = getCandidateStringValue();

        return value.length() > 0;
    }

    @Override
    public String formatMessage(String message) {
        return getCandidateName() + " - " + MessageFormat.format(message, getAttributeName());
    }
}
